import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/assets/global.less'
import axios from 'axios'

// 导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
// 导入富文本编辑器的样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// 全局注册富文本编辑器
Vue.use(VueQuillEditor)

Vue.use(ElementUI)

axios.defaults.baseURL = 'http://www.liulongbin.top:3008'

Vue.prototype.$http = axios

axios.interceptors.request.use(function(config) {
  // console.log(config)
  const token = store.state.user.token
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function(error) {
  return Promise.reject(error)
})

axios.interceptors.response.use(function(response) {
  // console.log('我拿了数据')
  return response
}, function(error) {
  // console.log('我报错了')
  // console.log(error)
  if (error.response.status === 401) {
    store.commit('user/updateToken', '')
    router.push('/login')
    Message.error(error.response.data.message)
  }
  return Promise.reject(error)
})

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
